package cs419.magnusmi.trackitcs;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class addStart extends ActionBarActivity {

    EditText id;
    Button scanBtn;
    Button addBtn;
    Button cancelBtn;
    public String barData;

    //session vars
    String user;
    String API;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_start);

        id = (EditText) findViewById(R.id.addId);
        scanBtn = (Button) findViewById(R.id.scan_Button);
        addBtn = (Button) findViewById(R.id.addBtn);
        cancelBtn = (Button) findViewById(R.id.cancelBtn);

        Intent i = getIntent();
        user = i.getStringExtra("user");
        API = i.getStringExtra("API");


        addBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                //Launching add Activity
                verifyAndLaunch();
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                //Launching add Activity
                Intent i = new Intent(getApplicationContext(), Inventory.class);
                Utility.addToIntent(i, user, API);
                startActivity(i);
            }
        });
        //Pulls up the ZXing barcode scanner on the phone
        try {
            Button scanner = (Button)findViewById(R.id.scanBtn);
            scanner.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    startActivityForResult(intent, 0);
                }

            });

        } catch (ActivityNotFoundException anfe) {
            Log.e("onCreate", "Scanner Not Found", anfe);
        }

    }

    //Handle variables captured from the ZXing barcode scanner
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

                barData = intent.getStringExtra("SCAN_RESULT");
                id.setText(barData);//set's scanned data into text field

                //just displays on screen what was scanned
                Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format , Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 25, 400); //here it is (context, text, duration)
                toast.show();

            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
                Toast toast = Toast.makeText(this, "Scan was Cancelled!", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 25, 400); //here it is (context, text, duration)
                toast.show();

            }

        }


    }


    /**
     * Method to validate input as a number and proceed.
     */
    private void verifyAndLaunch(){
        String newId = id.getText().toString();

        id.setError(null);

        boolean cancel = false;
        View focusView = null;

        //confirm input is not empty
        if (TextUtils.isEmpty(newId)){
            cancel = true;
            id.setError(getString(R.string.error_field_required));
            focusView = id;
        }

        //if not empty, confirm input is an integer
        else {
            try {
                Integer.parseInt(newId);
            }
            catch (NumberFormatException e){
                cancel = true;
                id.setError(getString(R.string.number_format_improper));
                focusView = id;
            }
        }

        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            focusView.requestFocus();
        }
        else{

            Intent i = new Intent(getApplicationContext(), addDetail.class);
            i.putExtra("newItemID", newId);
            Utility.addToIntent(i, user, API);
            startActivity(i);

        }
    }



    @Override
    public boolean onCreateOptionsMenu (Menu menu)  {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.logout:
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                //clear stack to enforce logout (http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l)
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_CLEAR_TASK |
                        Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
                return true;
            case R.id.inventory_menu:
                Intent j = new Intent(getApplicationContext(), Inventory.class);
                Utility.addToIntent(j, user, API);
                startActivity(j);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}
